<?php
namespace app\api\controller;

use app\api\model\UserModel;
use app\BaseController;
use think\Validate;

class User extends  BaseController
{
    //注册
    public  function  register()
    {
        //接受
        $data = input('post.');
        //验证(验证器)
        $validate = new Validate();
        $result = $validate->scene('register');
        if(!$result){
            echo $validate->getError();
        }
        //判断账号有没有被注册过(查询)
        $user = UserModel::where('mobile',$data['mobile'])->find();
        if($user){
            echo '该手机号已被注册';
        }
        //处理密码
        $randStr = createRandStr();
        $password = md5($data['password'].$randStr);
        $data['salt'] = $randStr;
        $data['password'] = $password;
        //入库
        $data['create_time'] = time();
        if(UserModel::create($data)){
            return success('注册成功');
        }else{
            return error('注册失败');
        }
    }
}